<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="__PUBLIC__/layui/css/layui.css"  media="all">
    <link rel="stylesheet" href="__PUBLIC__/font-awesome/css/font-awesome.min.css" media="all" />
    <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->

</head>
<body>
<div style="margin-top: 20px;">
</div>

<form class="layui-form" id="admin">
    <div class="layui-form-item">
        <label class="layui-form-label">主题名称</label>
        <div class="layui-input-inline" style="width: 300px">
            <input name="name" lay-verify="required" {notempty name="$info.name"}value="{$info.name}"{/notempty} placeholder="请输入" autocomplete="off" class="layui-input" type="text">
        </div>
    </div>


    <div class="layui-form-item">
        <label class="layui-form-label">排序</label>
        <div class="layui-input-inline" style="width: 150px">
            <input name="sortNum" lay-verify="required" {notempty name="$info.sortNum"}value="{$info.sortNum}"{/notempty} placeholder="请输入数字" autocomplete="off" class="layui-input" type="number">
        </div>
    </div>


    <div class="layui-upload" id="upload-thumb">
        <label class="layui-form-label">展示图片</label>
        <button type="button" class="layui-btn" id="thumb">上传图片</button>
        <div class="layui-upload-list">
            <label class="layui-form-label"></label>
            <img class="layui-upload-img" id="demo1" width="150" height="150" {notempty name='$info.attachment.filepath'}src="{$info.attachment.filepath}"{/notempty}>
            <p id="demoText"></p>
        </div>
    </div>


    <div class="layui-form-item " >
        <label class="layui-form-label">标签</label>
        <div class="layui-input-inline" style="width: 400px">
            <select id="tag" lay-filter="addtheme" lay-verify="required" lay-search>
                {volist name="tag" id="vo"}
                <option value="{$vo.tag_id}">{$vo.tag_name}</option>
                {/volist}
            </select>
        </div>
        <span id="addtheme" data-name="{notempty name='$tag[0]["tag_id"]'}{$tag[0]["tag_name"]}{/notempty}" data-id="{notempty name='$tag[0]["tag_id"]'}{$tag[0]["tag_id"]}{/notempty}" class="layui-btn layui-btn-danger layui-btn-sm" style="margin-top: 4px">添加</span>
    </div>



    <div class="layui-form-item">
        <label class="layui-form-label">已选标签</label>
        <div class="layui-input-inline" style="width: 500px" id="tagnames">
            {notempty name="$tagdata"}
            {volist name="tagdata" id="vo"}
            <span class="layui-btn layui-btn-sm layui-btn-normal deltripspan" data-id="{$vo['tag_id']}" style="margin: 5px 10px">{$vo['tag_name']}<i class="layui-icon"></i></span>
            {/volist}
            {/notempty}
        </div>
    </div>

    <input name="tag_id" id="tagids" value="{notempty name="$info.tag_id"}{$info.tag_id}{/notempty}" type="hidden">


    <div class="layui-upload" id="upload-thumb1">
        <label class="layui-form-label">背景图片</label>
        <button type="button" class="layui-btn" id="thumb1">上传背景</button>
        <div class="layui-upload-list">
            <label class="layui-form-label"></label>
            <img class="layui-upload-img" id="demo11" width="150" height="150" {notempty name='$info.attachment1.filepath'}src="{$info.attachment1.filepath}"{/notempty}>
            <p id="demoText1"></p>
        </div>
    </div>



    <div class="layui-form-item">
        <label class="layui-form-label">描述</label>
        <div class="layui-input-inline" style="width:60%;">
            <textarea placeholder="描述" class="layui-textarea" name="describe">{notempty name="$info.describe"}{$info.describe}{/notempty}</textarea>
        </div>
    </div>


    <div class="layui-form-item">
        <label class="layui-form-label">启用状态</label>
        <div class="layui-input-inline">
            <select name="status">
                <option value="1" {notempty name='$info'}{if !empty($info.status)&&$info.status=='1'}selected{/if}{/notempty}>启用</option>
                <option value="2" {notempty name='$info'}{if !empty($info.status)&&$info.status=='2'}selected{/if}{/notempty}>停用</option>
            </select>
        </div>
    </div>


    {notempty name="$info"}
    <input type="hidden" name="id" value="{$info.id}">
    {/notempty}


    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="admin">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>

</form>


<script src="__PUBLIC__/layui/layui.js"></script>
<script src="__PUBLIC__/jquery/jquery.min.js"></script>
<script>
    var message;
    layui.config({
        base: '__JS__/',
        version: '1.0.1'
    }).use(['app', 'message'], function() {
        var app = layui.app,
            $ = layui.jquery,
            layer = layui.layer;
        //将message设置为全局以便子页面调用
        message = layui.message;
        //主入口
        app.set({
            type: 'iframe'
        }).init();
    });
</script>

<script>
    layui.use(['layer', 'form'], function() {
        var layer = layui.layer,
            $ = layui.jquery,
            form = layui.form;
        $(window).on('load', function() {
            form.on('submit(admin)', function(data) {
                $.ajax({
                    url:"{:url('admin/theme/addtheme')}",
                    data:$('#admin').serialize(),
                    type:'post',
                    async: false,
                    success:function(res) {
                        layer.msg(res.msg);
                        if(res.code == 1) {
                            setTimeout(function(){
                                var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                                parent.layer.close(index); //再执行关闭
                            },1500)
                        }
                    }
                })
                return false;
            });
        });


        form.on('select(addtheme)', function(data){
            var tripid=data.value;
            $("#addtheme").attr("data-id",tripid);
        });

        $("#addtheme").on("click",function () {
            var tripid= $("#addtheme").attr("data-id");
            var tagids=$("#tagids").val();
            var ids=tagids.split(",");

            if (tripid==999999){
                tagids=999999;
                var tagspan='<span class="layui-btn layui-btn-sm layui-btn-normal deltripspan" data-id="999999" style="margin: 5px 10px">全部<i class="layui-icon"></i></span>';
                $("#tagnames").html(tagspan);
            }else{

                for(var i=0 ; i < ids.length; i++){
                    if(tripid==ids[i]){
                        layer.msg("不能重复添加商品");
                        return false;
                    }
                    if(ids[i]==999999){
                        tagids=999999;
                        var tagspan='<span class="layui-btn layui-btn-sm layui-btn-normal deltripspan" data-id="999999" style="margin: 5px 10px">全部<i class="layui-icon"></i></span>';
                        $("#tagnames").html(tagspan);
                        $("#tagids").val(tagids);
                        return false;
                    }
                }
                var tripname="";
                $("#tag").find("option").each(function () {
                    if($(this).val()==tripid){
                        tripname=$(this).html();
                    }
                });

                var tagspan='<span class="layui-btn layui-btn-sm layui-btn-normal deltripspan" data-id="'+tripid+'" style="margin: 5px 10px">'+tripname+'<i class="layui-icon"></i></span>';

                if(!tagids){
                    tagids+=tripid;
                }else{
                    tagids+=','+tripid;
                }
                $("#tagnames").append(tagspan);
            }

            $("#tagids").val(tagids);
        });


        $(document).on("click",'.deltripspan',function () {
            var dataid=$(this).attr("data-id");
            var newid="";
            $("#tagnames").find("span").each(function(){
                var id = $(this).attr("data-id");
                if(dataid!==id){
                    newid += ","+id;
                }
            });
            if(newid.length>0){
                newid = newid.substr(1,newid.length);
            }

            $("#tagids").val(newid);
            $(this).remove();
        })
    });
</script>
<script>
    layui.use('upload', function(){
        var upload = layui.upload;
        //执行实例
        var uploadInst = upload.render({
            elem: '#thumb' //绑定元素
            ,url: "{:url('common/upload')}" //上传接口
            ,done: function(res){
                //上传完毕回调
                layer.closeAll('loading');
                if(res.code == 2) {
                    $('#demo1').attr('src',res.src);
                    $('#upload-thumb').append('<input type="hidden" name="thumb" value="'+ res.id +'">');
                } else {
                    layer.msg(res.msg);
                }
            }
            ,error: function(){
                //请求异常回调
                //演示失败状态，并实现重传
                layer.closeAll('loading');
                var demoText = $('#demoText');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-mini demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function(){
                    uploadInst.upload();
                });
            }
            ,before:function () {
                layer.load(2);
            }
        });

        var uploadInst1 = upload.render({
            elem: '#thumb1' //绑定元素
            ,url: "{:url('common/upload')}" //上传接口
            ,done: function(res){
                //上传完毕回调
                layer.closeAll('loading');
                if(res.code == 2) {
                    $('#demo11').attr('src',res.src);
                    $('#upload-thumb1').append('<input type="hidden" name="thumb1" value="'+ res.id +'">');
                } else {
                    layer.msg(res.msg);
                }
            }
            ,error: function(){
                //请求异常回调
                //演示失败状态，并实现重传
                layer.closeAll('loading');
                var demoText = $('#demoText1');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-mini demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function(){
                    uploadInst1.upload();
                });
            }
            ,before:function () {
                layer.load(2);
            }
        });
    });
</script>
</body>
</html>
